package l1j.server.server.templates;

import java.util.Calendar;

import l1j.opqlo.NewSystem.城堡技能.相關列表;
import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.Server.L1World.WorldClan;
import l1j.server.server.datatables.CastleTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_Sound;
import l1j.server.server.serverpackets.S_SystemMessage;
import static l1j.server.server.model.skill.L1SkillId.狂暴打擊;
import static l1j.server.server.model.skill.L1SkillId.鋼鐵防禦;
import static l1j.server.server.model.skill.L1SkillId.賞金獵人;
import static l1j.server.server.model.skill.L1SkillId.狩獵專長;

public class L1Castle {

	private final int _id;

	private final String _name;

	private Calendar _warTime;

	private int _taxRate;

	private int _publicMoney;

	private byte _castleLv, _skillPoint;

	private int _castleExp, _lastCastleExp;

	public byte _skillAtk, _skillDef, _skillDrop, _skillExp;

	private int srcClanId;

	public L1Castle(final int id, final String name) {
		this._id = id;
		this._name = name;
	}

	public synchronized void addCastleExp(final int exp) {
		this._lastCastleExp += exp;
		if (this._lastCastleExp >= 相關列表.getInstance().MaxExp) {
			this._lastCastleExp = 相關列表.getInstance().MaxExp;
		}
		if (this._castleExp != this._lastCastleExp) {
			this._castleExp = this._lastCastleExp;
			int count = -1;
			for (byte lv = this._castleLv; this._castleExp >= 相關列表
					.getInstance()._getExpByLv[lv]; lv++) {
				this._castleLv = lv;
				count++;
				if (this._castleLv >= 相關列表.getInstance().MaxLevel) {
					break;
				}
			}
			this._skillPoint += count;
			CastleTable.getInstance().updateCastleStat(this);
		}
	}

	public synchronized void addSkillLv(final byte type) {
		if (this.getSkillList()[type] >= 10) {
			return;
		}
		if (type == 1) {
			this._skillAtk++;
		} else if (type == 2) {
			this._skillDef++;
		} else if (type == 3) {
			this._skillDrop++;
		} else if (type == 4) {
			this._skillExp++;
		}
		this.setSkillPoint((byte) (this._skillPoint - 1));
		CastleTable.getInstance().updateCastleSkill(this);
	}

	public void checkClanid(final int newClanid) {
		if (this.srcClanId == 0) {
			return;
		}
		String clanName = "";
		for (final L1Clan clan : WorldClan.get().getAllClans()) {
			if (clan.getClanId() == this.srcClanId) {
				clanName = clan.getClanName();
				break;
			}
		}
		if (this.srcClanId == newClanid) {
			for (final L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				pc.sendPackets(new S_SystemMessage("\\aL血盟 " + clanName+ " 保住了基業，"));
				pc.sendPackets(new S_SystemMessage("\\aL城堡的等級得以留存。"));
			}
			return;
		}

		this._skillAtk = 0;
		this._skillDef = 0;
		this._skillDrop = 0;
		this._skillExp = 0;
		this._castleLv = 0;
		this._skillPoint = 0;
		this._castleExp = 0;
		this._lastCastleExp = 0;
		CastleTable.getInstance().resetCastleAllStat(this);
		for (final L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			pc.sendPackets(new S_SystemMessage("\\aL血盟 " + clanName+ " 的城堡被奪去，"));
			pc.sendPackets(new S_SystemMessage("\\aL所有的基業也消失殆盡。"));
		}
	}

	public boolean checkSkillLv(final byte type) {

		return this.getSkillList()[type] == 0;
	}

	public int getCastleExp() {
		return this._castleExp;
	}

	public byte getCastleLv() {
		return this._castleLv;
	}

	public int getId() {
		return this._id;
	}

	public String getName() {
		return this._name;
	}

	public int getNextLvNeedExp() {
		final int needExp;
		if (this._castleLv >= 相關列表.getInstance().MaxLevel) {
			needExp = 0;
		} else {
			needExp = 相關列表.getInstance()._getExpByLv[this._castleLv + 1]- this._castleExp;
		}
		return needExp;
	}

	public int getPublicMoney() {// opqlo 城主存領錢可重複領取BUG修正
		if (this._publicMoney < 0) {
			return 0;
		}
		return this._publicMoney;

	}

	public byte[] getSkillList() {
		final byte[] value = { 0, this._skillAtk, this._skillDef,
				this._skillDrop, this._skillExp };
		return value;
	}

	public byte getSkillPoint() {
		return this._skillPoint;
	}

	public int getTaxRate() {
		return this._taxRate;
	}

	public Calendar getWarTime() {
		return this._warTime;
	}

	public void setCastleExp(final int exp) {
		this._lastCastleExp = exp;
		this._castleExp = this._lastCastleExp;
	}

	public void setCastleLv(final byte lv) {
		this._castleLv = lv;
	}

	public void setPublicMoney(final int i) {
		this._publicMoney = i;
	}

	public void setSkillPoint(final byte point) {
		this._skillPoint = point;
	}

	public void setSrcClanid(final int id) {
		this.srcClanId = id;
	}

	public void setTaxRate(final int i) {
		this._taxRate = i;
	}

	public void setWarTime(final Calendar i) {
		this._warTime = i;
	}

	public void useCastleSkill(final byte type, final L1PcInstance pc,
			final L1NpcInstance npc, int time) {
		pc._CastleweaponSkillDmg = 0;
		pc._CastleMagicModDmg = 0;
		pc._CastleExtraDef = 0;
		pc._CastleDmgReduction = 0;
		pc._CastleDropRate = 0;
		pc._CastleExpRate = 0;
		pc.killSkillEffectTimer(狂暴打擊);
		pc.killSkillEffectTimer(鋼鐵防禦);
		pc.killSkillEffectTimer(賞金獵人);
		pc.killSkillEffectTimer(狩獵專長);
		int skillid = 0, gfxid = 0, soundid = 0;
		String msg = null;

		switch (type) {
		case 1:
			if(this._skillAtk > 0){
				pc._CastleweaponSkillDmg = 相關列表.getInstance()._normalAtkList.get(this._skillAtk);
				pc._CastleMagicModDmg = 相關列表.getInstance()._MagicAtkList.get(this._skillAtk);
				skillid = 狂暴打擊;
				msg = "\\aH狂暴打擊的效果，已經覆蓋你的全身。LV:"+this._skillAtk;
				gfxid = 418;
				soundid = 9801;
			}
			break;
		case 2:
			if( this._skillDef > 0){
				pc._CastleExtraDef = 相關列表.getInstance()._ExtraDefList.get(this._skillDef);
				pc._CastleDmgReduction = 相關列表.getInstance()._DmgReductionList.get(this._skillDef);
				skillid = 鋼鐵防禦;
				msg = "\\aH身上充滿了鋼鐵防禦的力量。LV:"+this._skillDef;
				gfxid = 419;
				soundid = 9802;
			}
			break;
		case 3:
			if(this._skillDrop >0){
				pc._CastleDropRate = 相關列表.getInstance()._DropRateList.get(this._skillDrop);
				skillid = 賞金獵人;
				msg = "\\aH你領悟了賞金獵人的本領。LV:"+this._skillDrop;
				gfxid = 5763;
				soundid = 9803;
			}
			break;
		case 4:
			if(this._skillExp >0){
				pc._CastleExpRate = 相關列表.getInstance()._ExpRateList.get(this._skillExp);
				skillid = 狩獵專長;
				msg = "\\aH經驗的取得變得更加容易。LV:"+this._skillExp;
				gfxid = 424;
				soundid = 9804;
			}
			break;
		default:
			skillid = 0;
			gfxid = 0;
			msg = null;
			soundid = 0;
			break;
		}
		if(skillid !=0){
			time *= 1000;
			pc.setSkillEffect(skillid, time);
			if (npc != null) {
				npc.broadcastPacketX8(new S_Sound(soundid));
				npc.broadcastPacketX8(new S_SkillSound(pc.getId(), gfxid));
			} else {
				pc.sendPacketsX8(new S_Sound(soundid));
				pc.sendPacketsX8(new S_SkillSound(pc.getId(), gfxid));
			}
			pc.sendPackets(new S_SystemMessage(msg));
		}
	}

}
